﻿using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.PLCConfigApp.Dtos;
using PLCS.Application.Contracts.PLCReadDataApp.Dtos;
using PLCS.Client.IService.ConfigManager;
using Prism.Services.Dialogs;
using System;
using System.Collections;
using System.Collections.Generic;
using Volo.Abp.ObjectMapping;
using System.Collections.ObjectModel;
using Prism.Commands;
using PLCS.Domain.PLCConfigs;
using System.Linq;
using PLCS.App.Shared.Extensions;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Shared.Common.Constant;
using PLCS.Client.IService.LogManager;

namespace PLCS.App.ConfigManager.ViewModels.PLCReadData;

internal class PLCReadDataDetailViewModel : HostDialogViewModel
{
    public PLCReadDataDetailViewModel(IObjectMapper<ConfigManagerModule> objectMapper, IPLCReadDataService pLCReadDataService, IPLCConfigService pLCConfigService, IOperationLogService logShowService)
    {
        _objectMapper = objectMapper;
        _pLCReadDataService = pLCReadDataService;
        _pLCConfigService = pLCConfigService;
        this._logShowService = logShowService;
        View = "PLC字段配置弹窗";
    }

    private PLCReadDataDto _model;
    private readonly IObjectMapper<ConfigManagerModule> _objectMapper;
    private readonly IPLCReadDataService _pLCReadDataService;
    private readonly IPLCConfigService _pLCConfigService;
    private readonly IOperationLogService _logShowService;
    private PLCConfigDto _pLCModel;

    public PLCConfigDto PLCModel
    {
        get { return _pLCModel; }
        set { _pLCModel = value; }
    }

    public IEnumerable<PLCConfigDto> PLCConfigs { get; set; }

    public PLCReadDataDto Model
    {
        get { return _model; }
        set { _model = value; }
    }

    private ObservableCollection<string> _plcModels;

    public ObservableCollection<string> PLCModels
    {
        get { return _plcModels; }
        set { _plcModels = value; RaisePropertyChanged(); }
    }

    public DelegateCommand RefreshPLCCommand => new DelegateCommand(async () =>
    {
        PLCConfigs = await _pLCConfigService.GetListAsync();
        PLCModels = PLCConfigs.Select(x => x.PLCNo).ToObservable();
    });

    protected override async void Save()
    {
        try
        {
            SavebuttonIsEnabled = false;
            var plcReadData = _objectMapper.Map<PLCReadDataDto, PLCReadDataEditDto>(Model);
            if (plcReadData.Id != Guid.Empty)
            {
                await _pLCReadDataService.UpdateAsync(plcReadData);
                Log("修改", plcReadData);
            }
            else
            {
                await _pLCReadDataService.CreateAsync(plcReadData);
                Log("新增", plcReadData);
            }

            ShowMessage("操作成功");
            base.Save();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
        finally { SavebuttonIsEnabled = true; }
    }

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        if (parameters.ContainsKey("Value"))
        {
            Model = parameters.GetValue<PLCReadDataDto>("Value");
            Title = "编辑";
        }
        else
        {
            Title = "新增";
            Model = new PLCReadDataDto();
        }
    }
}